package ${package.ServiceImpl};

import com.Java.train.common.response.CommonResp;
import com.Java.train.business.entity.DTO.${table.entityName!}DTO;
import com.Java.train.business.req.${table.entityName!}QueryReq;
import com.Java.train.common.exception.BusniessException;
import com.Java.train.common.exception.BusniessExceptionEnum;
import com.Java.train.common.response.PageResp;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import ${superServiceImplClassPackage};
import org.springframework.stereotype.Service;
import com.Java.train.common.context.LoginMemberContext;
import com.Java.train.common.utils.SnowFlakeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.util.CollectionUtils;
import cn.hutool.core.date.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Arrays;
import java.util.Collections;
/**
* ${table.comment!} 服务实现类
*
* @author ${author}
* @since ${date}
*/
@Service
<#if kotlin>
    open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} {

    }
<#else>
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} {

    private static final Logger LOG = LoggerFactory.getLogger(${table.entityName}Service.class);

    @Autowired
    ${table.entityName}Mapper ${table.entityName?uncap_first}Mapper;


    @Override
    public PageResp<${table.entityName}DTO> queryList(${table.entityName}QueryReq ${table.entityName?uncap_first}QueryReq){
        // 1.获取当前用户id信息
        //        Long id = LoginMemberContext.getId();
        // 2.查询数据库中是否存在当前信息（id）
        LOG.info("查询页码：{}", ${table.entityName?uncap_first}QueryReq.getPage());
        LOG.info("每页条数：{}", ${table.entityName?uncap_first}QueryReq.getSize());
        PageHelper.startPage(${table.entityName?uncap_first}QueryReq.getPage(),${table.entityName?uncap_first}QueryReq.getSize());
        List<${table.entityName}> ${table.entityName?uncap_first}s = list();
        if(${table.entityName?uncap_first}s == null || CollUtil.isEmpty(${table.entityName?uncap_first}s)){
            throw new BusniessException(BusniessExceptionEnum.MEMBER_PASSENGER_NOT_EXITS);
        }

        // 3 分析分页信息
        // 3.1 获取分页总条数和页数
        PageInfo<${table.entityName}> pageInfo = new PageInfo<>(${table.entityName?uncap_first}s);
        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());

        // 3.2 转为封装后的返回值并封装到分页返回值中
        List<${table.entityName}DTO> list = BeanUtil.copyToList(${table.entityName?uncap_first}s, ${table.entityName}DTO.class);
        PageResp<${table.entityName}DTO> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);
        // 4.返回查询到的信息
        return pageResp;
    }

    @Override
    public CommonResp<Object> modify(${table.entityName} ${table.entityName?uncap_first}) {
        Long id = ${table.entityName?uncap_first}.getId();
        DateTime dateTime = DateTime.now();
        if(ObjectUtil.isNull(id)){
            ${table.entityName?uncap_first}.setId(SnowFlakeUtil.getSnowFlakeNextId());
            ${table.entityName?uncap_first}.setCreateTime(dateTime);
            ${table.entityName?uncap_first}.setUpdateTime(dateTime);
            boolean save = save(${table.entityName?uncap_first});
            return new CommonResp<>(save);
        } else {
            ${table.entityName?uncap_first}.setUpdateTime(dateTime);
            boolean update = updateById(${table.entityName?uncap_first});
            return new CommonResp<>(update);
        }
    }

    @Override
    public CommonResp<Object> remove(String ids) {
        if(StrUtil.isNotEmpty(ids)){
            String[] array = ids.split(",");
            if(!CollectionUtils.isEmpty(Arrays.asList(array))) {
                return new CommonResp<>(${table.entityName?uncap_first}Mapper.deleteBatchIds(Arrays.asList(array)));
            }
        }
        return new CommonResp<>(Collections.EMPTY_LIST);
    }

}


</#if>
